package com.culturesalon.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.culturesalon.dao.CategoryDAO;
import com.culturesalon.dao.CategoryDAOImpl;
import com.culturesalon.dao.GoodsDAO;
import com.culturesalon.dao.GoodsDAOImpl;
import com.culturesalon.dto.Category;
import com.culturesalon.dto.Goods;
import com.culturesalon.util.StringUtil;

/**
 * Servlet implementation class GoodsUpdate
 */
@WebServlet("/goodsupdate.do")
public class GoodsUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GoodsUpdate() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String id = request.getParameter("id");
		String action = request.getParameter("actionx");
		CategoryDAO categoryDao=new CategoryDAOImpl();
		List<Category> categories=categoryDao.findAll();
		if (StringUtil.isEmpty(id)) {
			request.getRequestDispatcher("goodslist.do").forward(request,
					response);
		} else {
			if (action != null && action.equals("update")) {
				GoodsDAO goodsdao = new GoodsDAOImpl();
				Goods goods = goodsdao.get(Integer.parseInt(id));
				request.setAttribute("categories", categories);
				request.setAttribute("goods", goods);
				request.setAttribute("mainpage", "retestgoodsupdate.jsp");
				request.getRequestDispatcher("admin/adminindex.jsp").forward(
						request, response);

			} else {
				request.setCharacterEncoding("UTF-8");

				String goodNumber = request.getParameter("goodsNumber");
				String goodName = request.getParameter("goodsName");
				String brandName = request.getParameter("brandName");
				String goodsPictureUrl = request
						.getParameter("goodsPictureUrl");
				String retailPrice = request.getParameter("retailPrice");
				String marketPrice = request.getParameter("marketPrice");
				String goodsNature = request.getParameter("goodsNature");
				String goodsDesc = request.getParameter("goodsDesc");
				String goodsTag = request.getParameter("goodsTag");
				String goodsInventory = request.getParameter("goodsInventory");
				String shelfDate = request.getParameter("shelfDate");
				String updateDate = request.getParameter("updateDate");
				String inventoryWarn = request.getParameter("inventoryWarn");
				String recommend = request.getParameter("recommend");
				String click = request.getParameter("click");
				String categoryId = request.getParameter("categoryId");
				if (!StringUtil.isEmpty(goodName)) {
					// &&!StringUtil.isEmpty(categoryId)
					Goods good = new Goods();
					good.setGoodsNumber(goodNumber);
					good.setGoodsName(goodName);
					good.setBrandName(brandName);
					good.setGoodsPictureUrl(goodsPictureUrl);
					good.setRetailPrice(Double.parseDouble(retailPrice));
					good.setMarketPrice(Double.parseDouble(marketPrice));
					good.setGoodsNature(goodsNature);
					good.setGoodsDesc(goodsDesc);
					good.setGoodsTag(goodsTag);
					good.setGoodsInventory(Integer.parseInt(goodsInventory));
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

					try {
						good.setShelfDate(sdf.parse(shelfDate));
						good.setUpdateDate(sdf.parse(updateDate));
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					good.setInventoryWarn(Integer.parseInt(inventoryWarn));
					good.setRecommend(Boolean.parseBoolean(recommend));
					good.setClick(Integer.parseInt(click));
					good.setCategoryId(Integer.parseInt(categoryId));
					GoodsDAO goodsDAO = new GoodsDAOImpl();
					good.setId(Integer.parseInt(id));
					goodsDAO.update(good);
					//request.setAttribute("mainpage", "/CultureSalon/goodsupdate.do");
					request.getRequestDispatcher("goodslist.do")
							.forward(request, response);
				}
			}
		}
	}
}
